package com.monco.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.monco.common.response.ApiResult;
import com.monco.entity.RubbishRecord;
import com.monco.service.IRubbishRecordService;
import com.monco.vo.RubbishRecordListVo;
import com.monco.vo.RubbishRecordVo;
import io.swagger.annotations.Api;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author com/monco
 * @since 2020-03-11
 */
@Api(tags = "扔垃圾记录模块API")
@RestController
@RequestMapping("/rubbish-record")
public class RubbishRecordController {

    @Resource
    private IRubbishRecordService rubbishRecordService;


    /**
     * 新增RubbishRecordList方法
     * 用户用户进行丢弃垃圾时的记录
     * @param rubbishRecordListVo
     */
    @PostMapping
    public ApiResult saveRubbishRecordList(@RequestBody RubbishRecordListVo rubbishRecordListVo) {
        List<RubbishRecord> rubbishRecordList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(rubbishRecordListVo.getRubbishRecordVoList())) {
            for (RubbishRecordVo rubbishRecordVo : rubbishRecordListVo.getRubbishRecordVoList()) {
                RubbishRecord rubbishRecord = new RubbishRecord();
                BeanUtils.copyProperties(rubbishRecordVo, rubbishRecord);
                rubbishRecordList.add(rubbishRecord);
            }
            rubbishRecordService.saveRubbishRecord(rubbishRecordList, rubbishRecordListVo.getUserId());
        }
        return ApiResult.ok();
    }

    /**
     * 修改RubbishRecord方法
     *
     * @param rubbishRecord
     * @return
     */
    @PutMapping
    public ApiResult update(@RequestBody RubbishRecord rubbishRecord) {
        rubbishRecordService.updateById(rubbishRecord);
        return ApiResult.ok();
    }

    /**
     * 查询列表
     *
     * @param rubbishRecord
     * @return
     */
    @GetMapping("list")
    public ApiResult list(RubbishRecord rubbishRecord) {
        return ApiResult.ok(rubbishRecordService.getRubbishRecordList(rubbishRecord));
    }

    /**
     * 查询分页
     *
     * @param current       当前页
     * @param size          一页多少数据
     * @param rubbishRecord
     * @return
     */
    @GetMapping("page")
    public ApiResult page(@RequestParam(required = false, defaultValue = "0") long current,
                          @RequestParam(required = false, defaultValue = "10") long size, RubbishRecord rubbishRecord) {
        return ApiResult.ok(rubbishRecordService.getRubbishRecordPage(new Page<RubbishRecord>().setCurrent(current).setSize(size), rubbishRecord));
    }

    /**
     * 删除方法
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public ApiResult delete(@RequestParam Long id) {
        rubbishRecordService.removeById(id);
        return ApiResult.ok();
    }

    /**
     * 按照id获取单条记录
     *
     * @param id
     * @return
     */
    @GetMapping
    public ApiResult getOne(@RequestParam Long id) {
        return ApiResult.ok(rubbishRecordService.getById(id));
    }
}
